

import 'dart:ui';


//底层绘制
final pen = Paint()..color = Color(0xff000000)..strokeWidth = 4;
final List<Offset?> points = [];
void main(){
  window.onBeginFrame = onBeginFrame;
  window.onPointerDataPacket = onPointerDataPacket;
  window.scheduleFrame();
}
void onBeginFrame(Duration duration){
  final recorder = PictureRecorder();
  final canvans = Canvas(recorder);
  canvans.drawColor(Color(0xffffffff), BlendMode.srcATop);
  for(int i=0;i<points.length-1;i++){
    if(points[i]!=null && points[i+1]!=null){
      canvans.drawLine(points[i]!, points[i+1]!, pen);
    }
  }
  final picture = recorder.endRecording();
  final sceneBulider = SceneBuilder()..addPicture(Offset.zero, picture);
  window.render(sceneBulider.build());
  window.scheduleFrame();
}
void onPointerDataPacket(PointerDataPacket packet){
  for(final data in packet.data){
    final pos = Offset(data.physicalX, data.physicalY);
    if(data.change == PointerChange.move){
      points.add(pos);
    }else if(data.change == PointerChange.up){
      points.add(null);
    }
  }
}
